//
//  SmallDecisionsXjdViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsXjdViewController.h"
#import "SmallDecisionsXjdChooseTemplateViewController.h"
#import "SmallDecisionsXjdComplateViewController.h"
#import "SmallDecisionsPlateView.h"
#import "SmallDecisionsXjdModel.h"

@interface SmallDecisionsXjdViewController ()<SmallDecisionsPlateViewDelegate>

@property (nonatomic, strong) SmallDecisionsPlateView *plateView;
@property (nonatomic, strong) SmallDecisionsXjdModel *xjdTemplateModel;
@property (nonatomic, strong) UIView  *xjdTemplateWrapView;

@property (nonatomic, strong) UILabel  *xjdTemplateTitleLabel;
@property (nonatomic, strong) UIButton *xjdTemplateBtn;

@end

@implementation SmallDecisionsXjdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"小决定";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self plateView];
    [self.xjdTemplateWrapView insertSubview:self.xjdTemplateTitleLabel aboveSubview:self.xjdTemplateBtn];
    [self.view layoutIfNeeded];
    [self loadSmallDecisionsXjdTemplateData];
}

- (void)loadSmallDecisionsXjdTemplateData {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *templateXjdPath = [documentPath stringByAppendingPathComponent:@"templateXjd.plist"];
    if (![fileManager fileExistsAtPath:templateXjdPath]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"templateXjd" ofType:@"json"];
        NSData *templateXjdData   = [NSData dataWithContentsOfFile:path];
        NSArray *templateXjdList = [NSJSONSerialization JSONObjectWithData:templateXjdData options:NSJSONReadingMutableContainers error:nil];
        templateXjdList = [NSArray yy_modelArrayWithClass:[SmallDecisionsXjdModel class] json:templateXjdData];
        for (SmallDecisionsXjdModel *xjdTemplateModel in templateXjdList) {
            if (xjdTemplateModel.isTemplateSelected) {
                self.xjdTemplateModel = xjdTemplateModel;
                break;
            }
        }
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSData *data = [templateXjdList plistData];
            [data writeToFile:templateXjdPath atomically:YES];
        });
    } else {
        NSArray *templateXjdList = [NSArray arrayWithContentsOfFile:templateXjdPath];
        templateXjdList = [NSArray yy_modelArrayWithClass:[SmallDecisionsXjdModel class] json:templateXjdList];
        for (SmallDecisionsXjdModel *xjdTemplateModel in templateXjdList) {
            if (xjdTemplateModel.isTemplateSelected) {
                self.xjdTemplateModel = xjdTemplateModel;
                break;
            }
        }
    }
}

- (SmallDecisionsPlateView *)plateView {
    if(!_plateView) {
        _plateView = [[SmallDecisionsPlateView alloc] init];
        _plateView.delegate = self;
        [self.view addSubview:_plateView];
        [_plateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.view);
            make.height.mas_equalTo(_plateView.mas_width);
        }];
    }
    
    return _plateView;
}


- (UIView *)xjdTemplateWrapView {
    if(!_xjdTemplateWrapView) {
        _xjdTemplateWrapView = [[UIView alloc] init];
        [self.view addSubview:_xjdTemplateWrapView];
        [_xjdTemplateWrapView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
        }];
    }
    
    return _xjdTemplateWrapView;
}

- (UILabel *)xjdTemplateTitleLabel {
    if(!_xjdTemplateTitleLabel) {
        _xjdTemplateTitleLabel = [[UILabel alloc] init];
        _xjdTemplateTitleLabel.font = SmallDecisionsSetBoldFontSize(16);
        [self.xjdTemplateWrapView addSubview:_xjdTemplateTitleLabel];
        [_xjdTemplateTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.xjdTemplateWrapView);
            make.top.mas_equalTo(20);
        }];
    }
    
    return _xjdTemplateTitleLabel;
}

- (UIButton *)xjdTemplateBtn {
    if(!_xjdTemplateBtn) {
        _xjdTemplateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_xjdTemplateBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _xjdTemplateBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
        _xjdTemplateBtn.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
        _xjdTemplateBtn.layer.cornerRadius = 25;
        _xjdTemplateBtn.backgroundColor = SmallDecisionsSecondBgColor;
        [_xjdTemplateBtn addTarget:self action:@selector(xjdTemplateBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.xjdTemplateWrapView addSubview:_xjdTemplateBtn];
        [_xjdTemplateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_lessThanOrEqualTo(self.view).mas_offset(-60);
            make.top.mas_equalTo(self.xjdTemplateTitleLabel.mas_bottom).mas_offset(-10);
            make.centerX.mas_equalTo(self.xjdTemplateWrapView);
            make.height.mas_equalTo(50);
            make.bottom.mas_equalTo(-10);
        }];
    }
    
    return _xjdTemplateBtn;
    
}

- (void)setXjdTemplateModel:(SmallDecisionsXjdModel *)xjdTemplateModel {
    _xjdTemplateModel = xjdTemplateModel;
    self.plateView.data = xjdTemplateModel.templateItems;
    [self.plateView startPlateDraw];
    self.xjdTemplateTitleLabel.text = xjdTemplateModel.templateIcon;
    [self.xjdTemplateBtn setTitle:xjdTemplateModel.templateTitle forState:UIControlStateNormal];
}

- (void)xjdTemplateBtnAction {
    SmallDecisionsXjdChooseTemplateViewController *xjdChooseTemplateVC = [[SmallDecisionsXjdChooseTemplateViewController alloc] init];
    weakSelf(self);
    xjdChooseTemplateVC.callback = ^(SmallDecisionsXjdModel * _Nonnull xjdTemplateModel) {
        weakself.xjdTemplateModel = xjdTemplateModel;
    };
    [SmallDecisionsNavigatorManager pushViewController:xjdChooseTemplateVC];
}

#pragma mark SmallDecisionsPlateViewDelegate
- (void)plateViewDidBegin:(SmallDecisionsPlateView *)plateView {
    NSInteger index = arc4random() % plateView.data.count;
    [plateView startPlateAnimationWithTargetIndex:index];
}

- (void)plateViewDidFinish:(SmallDecisionsPlateView *)plateView target:(NSInteger)target {
    NSString *complateContent = self.xjdTemplateModel.templateItems[target];
    SmallDecisionsXjdComplateViewController * xjdComplateVC = [[SmallDecisionsXjdComplateViewController alloc] init];
    xjdComplateVC.complateContent = complateContent;
    [SmallDecisionsNavigatorManager presentViewController:xjdComplateVC showNavigationBar:NO animationType:SmallDecisionsAnimationTypeAlert];
}

- (void)dealloc {
    [self.plateView resetPlate];
}

@end
